﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using ResxCrunch.Utils;
using ResxCrunch.Properties;

namespace ResxCrunch
{
    /// <summary>
    /// Select language to add form class
    /// </summary>
    public partial class FrmSelectLang2Add : Form
    {
        #region Properties
        /// <summary>
        /// Gets or sets the table containig the columns with the languages.
        /// </summary>
        /// <value>Table containig the columns with the languages</value>
        public DataTable DataSource
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the selected Languages
        /// </summary>
        /// <value>List containing the selected Languages.</value>
        public List<string> SelectedLanguages
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether the content default values should be copied into the new columns.
        /// </summary>
        /// <value><c>true</c> if copy default values otherwise, <c>false</c>.</value>
        public bool CopyDefaultValues
        {
            get;
            set;
        }

        /// <summary>
        /// Sets a value indicating whether this <see cref="FrmMain"/> is busy.
        /// </summary>
        /// <value><c>true</c> if busy; otherwise, <c>false</c>.</value>
        public bool Busy
        {
            set
            {
                if (value)
                {
                    lblBusy.Visible = true;
                    this.Refresh();
                }
                else
                {
                    lblBusy.Visible = false;
                }
            }
        }
        #endregion Properties

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmSelectLang2Add"/> class.
        /// </summary>
        public FrmSelectLang2Add()
        {
            InitializeComponent();
        }
        #endregion Initialization

        #region Private methods
        /// <summary>
        /// Loads the languages.
        /// </summary>
        private void LoadLanguages()
        {
            this.Busy = true;

            DataTable cultureData = new DataTable("Data");
            cultureData.Columns.Add(Enums.LanguageDataColumn.Code);
            cultureData.Columns.Add(Enums.LanguageDataColumn.Language);

            CultureInfo[] cultures = null;

            if (rbAll.Checked)
            {
                cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
            }

            if (rbNeutral.Checked)
            {
                cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
            }

            if (rbSpecific.Checked)
            {
                cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            }

            foreach (CultureInfo culture in cultures)
            {
                if (culture.Name.Length > 0)
                {
                    DataRow dr = cultureData.NewRow();

                    dr[Enums.LanguageDataColumn.Code] = culture.Name;
                    dr[Enums.LanguageDataColumn.Language] = culture.DisplayName;

                    cultureData.Rows.Add(dr);
                }
            }

            DataView dv = new DataView(cultureData);

            grdLanguages.DataSource = dv;

            grdLanguages.Sort(grdLanguages.Columns[Enums.LanguageDataColumn.Code], ListSortDirection.Ascending);

            this.Busy = false;
        }

        /// <summary>
        /// Loads the form.
        /// </summary>
        private void LoadForm()
        {
            switch (Settings.Default.FrmSelectLang2Add_SelectedRbIndex)
            {
                case 1:
                    rbNeutral.Checked = true;
                    break;
                case 2:
                    rbSpecific.Checked = true;
                    break;
                default:
                    rbAll.Checked = true;
                    break;
            }

            chkCopyDefault.Checked = Settings.Default.FrmSelectLang2Add_CopyDefault;
        }

        /// <summary>
        /// Saves the form.
        /// </summary>
        private void SaveForm()
        {
            if (rbAll.Checked)
            {
                Settings.Default.FrmSelectLang2Add_SelectedRbIndex = 0;
            }

            if (rbNeutral.Checked)
            {
                Settings.Default.FrmSelectLang2Add_SelectedRbIndex = 1;
            }

            if (rbSpecific.Checked)
            {
                Settings.Default.FrmSelectLang2Add_SelectedRbIndex = 2;
            }

            Settings.Default.FrmSelectLang2Add_CopyDefault = chkCopyDefault.Checked;
        }

        /// <summary>
        /// Updates the values.
        /// </summary>
        private void UpdateValues()
        {
            lblNrSelected.Text = string.Concat(grdLanguages.SelectedRows.Count, "/", grdLanguages.RowCount, " selected");
        }

        /// <summary>
        /// Filters the content of the datagrid
        /// </summary>
        private void Filter()
        {
            DataView dv = (DataView)grdLanguages.DataSource;

            if (txtNameFilter.Text.Length > 0)
            {
                string filter = string.Format("({0} LIKE '%{2}%') OR ({1} LIKE '%{2}%')", Enums.LanguageDataColumn.Code, Enums.LanguageDataColumn.Language, txtNameFilter.Text);

                dv.RowFilter = filter;
            }
            else
            {
                dv.RowFilter = string.Empty;
            }

            grdLanguages.DataSource = dv;
        }
        #endregion Private methods

        #region Events
        /// <summary>
        /// Handles the Load event of the FrmSelectLang2Add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectLang2Add_Load(object sender, EventArgs e)
        {
            this.LoadForm();
        }

        /// <summary>
        /// Handles the Shown event of the FrmSelectLang2Add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectLang2Add_Shown(object sender, EventArgs e)
        {
            this.LoadLanguages();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the SelectionChanged event of the grdLanguages control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void grdLanguages_SelectionChanged(object sender, EventArgs e)
        {
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCodeFilter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtCodeFilter_TextChanged(object sender, EventArgs e)
        {
            Filter();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            List<string> selectedCultures = new List<string>();

            foreach (DataGridViewRow row in grdLanguages.SelectedRows)
            {
                selectedCultures.Add(row.Cells[Enums.LanguageDataColumn.Code].Value.ToString());
            }

            this.SelectedLanguages = selectedCultures;

            this.CopyDefaultValues = chkCopyDefault.Checked;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbAll_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadLanguages();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbNeutral control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbNeutral_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadLanguages();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the rbSpecific control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbSpecific_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadLanguages();
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the FormClosing event of the FrmSelectLang2Add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void FrmSelectLang2Add_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveForm();
        }
        #endregion Events

        private void lnkSelectAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            grdLanguages.SelectAll();
        }
    }
}
